
$(function() {
	$("input[id^='mold_s']:visible").autocomplete(Test.mold);

	Tip.emp('入库员');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("入库员：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
	
	$("input[id^='material_s']:visible").autocomplete(Test.material);
		
	$("select[id^='category_']:visible").selectChain({
		target: $("select[id^='subclass_']:visible"),
		key: "code",
		value: "name",
		url: 'material.php?do=Material.ajax_category',
		data: { ajax: true }
	}).trigger('change');
});

var In = {
	dialog_cal_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=75', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"计算": function() {
						if (!confirm("确认是否开始计算？")) {
							return false;
						}
						
						var form = $("#form_add")[0];

						var body = { };
						body.emp = form.emp.value;
						body.date1 = form.date1.value;
						body.date2 = form.date2.value;

						body.content = form.content.value;

						$(this).dialog("close");

						$.post('material.php?do=In.calPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	clean_cache: function () {
		if (!confirm("确认是否清除缓存？")) {
			return false;
		}

		$.get('material.php?do=Amount.ajax_place', function(data) {
			alert("操作成功！");
		});
	},

	popup: function (e, p2, id, material) {
		$.cookie('id_', id);

		$.cookie('status_', status);
		
		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'material.php?do=In.edit&id=' + id;
		var c2 = 'material.php?do=In.view&id=' + id;
		var c3 = 'material.php?do=Material.view&id=' + material;

		var a1 = ['编辑信息', 'url', 'In.dialog_edit_', '650x400'];
		var a2 = ['物料信息', 'url', 'Common.dialog_view_', '600x360'];
		var a3 = ['材料信息', 'url', 'Common.dialog_view_', '600x360'];
		
		var b1 = ['退货', 'url', 'In.dialog_returned', '650x420'];
		var d1 = 'material.php?do=ReturnedItem.add&id=' + id;
		
		var s = c1 + ", " + c2 + ", " + c3 + ", " + d1;

		return this.popup_(e, [a1, a2, a3, b1], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'material.php?do=In.ss&id=' + id;
		var bb = ['修改状态', 'url', 'In.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=In.audit&item=188', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=In.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=In.audit&item=58', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.name = form.name.value;						
						body.standard = form.standard.value;

						body.amount = form.amount.value;
						body.unit = form.unit.value;
						body.price = form.price.value;
						
						body.amount3 = form.amount3.value;
						body.unit2 = form.unit2.value;
						
						body.mold = form.mold.value;
						body.date = form.date.value;

						body.content = form.content.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=In.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_returned: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=RecordIn.audit&item=63', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"退货": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];
								
						var body = { };
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						body.place = form.place.value;
						body.amount = form.amount.value;
						
						body.content = form.content.value;

						$(this).dialog("close");
						
						var url = 'material.php?do=ReturnedItem.addPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							// $(result).html(data);
							alert("退货成功！");
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	expunge_: function (form) {
		$.getJSON('material.php?do=In.audit&item=52', function(json) {
			if (json.status == 1) {
				expunge(form);
			} else {
				_error();
			}
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=In.delete' + p2;		
		var c9 = 'material.php?do=In.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	}
};